package com.example.administrator.pomelostreet.mine;

import android.text.TextUtils;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static com.tencent.open.web.security.a.a;

/**
 * Created by Administrator on 2017/2/10.
 */

public class OkhttpUtil {
    private static String url="http://api.youzibuy.com/tae_coin?myuid=130403735&tbuid=&device_id=8625" +
            "47034764826&platform=android&v=2.1.0&imei=862547034764826&bundleid=186&mode=0&app_id=7&v_" +
            "auth=3.1H1TOP05wx1TKqYm54t6VcDKXH%2BmSMGyU2gIrfw9jvA%3D&statinfo=eyJzb3VyY2UiOiItPiIsImNoY" +
            "W5uZWxpZCI6IjE4NiIsIm9zIjoiMiIsIm90Ijoi5Lit5Zu96IGU6YCaIiwiYXBuIjoiNCIsInVhIjoiTTUgTm90ZSIs" +
            "Im9zdmVyc2lvbiI6IjYuMCIsInNka3ZlcnNpb24iOjIzLCJ2IjoiMi4xLjAiLCJidWlsZHYiOiIyLjEuMCIsInVpZCI" +
            "6MTMwNDAzNzM1LCJvcGVudWRpZCI6ImU3MjRlNDg1LTk0MjMtNDc4NS1iMWQxLWM2NjdlNmM0NmJmMSIsIm1hYyI6I" +
            "jg2MjU0NzAzNDc2NDgyNiIsImFuZHJvaWRpZCI6IjM5MDE0MjE4YTc2NzJmNmYiLCJpbWVpIjoiODYyNTQ3MDM0Nz" +
            "Y0ODI2IiwiaW1zaSI6IjQ2MDAxMzU5MTMzMTIxNyJ9&myclient=0730210018600000&sdkversion=23&path=&" +
            "timestamp=&platform=android&v=2.1.0&device_id=862547034764826";
//    private static String json;


    public static String getjson(){
        Log.i("===================","getjson");
        Observable.create(new Observable.OnSubscribe<String>() {

            @Override
            public void call(Subscriber<? super String> subscriber) {
                Log.i("===================","getjson2");
                try {
                    //下载内容
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder().get().url(url).build();
                    Response response = client.newCall(request).execute();
                    if (response.isSuccessful()) {
                        String json = response.body().string();
                        Log.i("=======1========",""+json);
                        //处理结果
                        if (!TextUtils.isEmpty(json)) {
                            subscriber.onNext(json);
                            subscriber.onCompleted();
                        } else {
                            subscriber.onError(new Throwable("json为空!"));
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    subscriber.onError(e);
                }
            }
        }).subscribeOn(Schedulers.newThread())//相当于开线程--->工作.执行耗时任务.规定了被观察者所在的线程.
                .observeOn(AndroidSchedulers.mainThread())//调度到主线程--->规定了观察者所在的线程.

                .subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {
                        Log.i("===========", "下载成功");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("=========", "异常信息:" + e.getMessage());
                    }

                    @Override
                    public void onNext(String s) {

                        String json=s;
                        Log.i("=======2========",""+json);
                    }
                });

        return null;
    }
    public static Datas  parseJson(String json){
        if (!TextUtils.isEmpty(json)) {
            try {
                Datas datas = new Datas();
                Log.i("===============", "parseJson: "+"运行位置1");
                JSONObject object1 = new JSONObject(json);
                JSONArray array2 = object1.getJSONArray("item_list");
                JSONObject object2 = array2.getJSONObject(0);
                Log.i("===============", "parseJson: "+"运行位置2");
                JSONArray array = object2.getJSONArray("item_list");
                List<Items> itemses = new ArrayList<>();
                Log.i("===============", "parseJson: "+"运行位置3"+array.length());
                for (int i = 0; i < array.length(); i++) {
                    Items items = new Items();
                    JSONObject object3 = array.getJSONObject(i);
                    items.setName(object3.optString("name"));
                    items.setId(object3.optInt("id"));
                    items.setBrand_area_name(object3.optString("brand_area_name"));
                    items.setNeed_coin_amount(object3.optInt("need_coin_amount"));
                    items.setOriginal_price(object3.optDouble("original_price"));
                    items.setItem_id(object3.optInt("item_id"));
                    items.setPicture(object3.optString("picture"));
                    items.setSttag_text(object3.optString("sttag_text"));
                    items.setSttag_type(object3.optInt("sttag_type"));
                    itemses.add(items);
                }
                datas.setItemses(itemses);
                Log.i("=============", "parseJson: 位置4");
                List<Categorys> categoryses = new ArrayList<>();
                JSONArray array1 = object1.getJSONArray("category_list");
                Log.i("===============", "parseJson: "+"运行位置5"+array1.length());
                for (int i = 0; i < array1.length(); i++) {
                    Categorys categorys = new Categorys();
                    JSONObject object = array1.getJSONObject(i);
                    categorys.setName(object.optString("name"));
                    categorys.setRedirect_url(object.optString("redirect_url"));
                    categorys.setId(object.optInt("id"));
                    categorys.setPicture(object.optString("picture"));
                    categoryses.add(categorys);
                }
                datas.setCategoryses(categoryses);

                Log.i("================", "parseJson: "+datas.getItemses().size()+"-----"+datas.getItemses().size());
                return datas;
            } catch (JSONException e) {
                e.printStackTrace();
                Log.i("===============", "parseJson: "+"运行位置6");
            }
        }
        return null;
    }
}
